<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>编辑菜品</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

  </head>
  
  <body>
    <div class="x-body">
        <form class="layui-form">
            <input type="hidden" id="foodId" name="foodId" value="${editFood.foodId}">
            <input type="hidden" id="foodTypeCodeInp" name="foodTypeCodeInp" value="${editFood.foodTypeCode}">
            <div class="layui-form-item">
                <label for="foodName" class="layui-form-label">
                    <span class="x-red">*</span>菜品名
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="foodName" maxlength="10" value="${editFood.foodName}" name="foodName" required="" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>菜品类型
                </label>
                <div class="layui-input-inline">
                    <select name="foodTypeCode">

                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="foodPrice" class="layui-form-label">
                    <span class="x-red">*</span>价格
                </label>
                <div class="layui-input-inline">
                    <input type="number" id="foodPrice" name="foodPrice" required="" value="${editFood.foodPrice}" lay-verify="price"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item" id="thumb-box">
                <label  class="layui-form-label">
                    <span class="x-red">*</span>菜品图片
                </label>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn" id="upload">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                </div>
                <input type="hidden" name="foodImg" value="${editFood.foodImg}" lay-verify="required">
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                </label>
                <img id="thumb" src="/upload/${editFood.foodImg}" width="380px" height="280px" alt="">
            </div>
          <div class="layui-form-item">
              <label  class="layui-form-label">
              </label>
              <button  class="layui-btn" lay-filter="edit" lay-submit="">
                  确认编辑
              </button>
          </div>
      </form>
    </div>
    <script>
        layui.use(['form','layer','upload'], function(){
            $ = layui.jquery;
            var form = layui.form
            ,layer = layui.layer;

            var upload = layui.upload;

            // 文件上传
            //执行实例
            var uploadInst = upload.render({
                elem: '#upload' //绑定元素
                ,url: '${pageContext.request.contextPath}/food?m=upload' //上传接口
                ,done: function(res){
                    //上传完毕回调
                    if (res.code === 200){
                        $("#thumb").attr("src", "/upload/"+res.data);
                        $("#thumb").show();
                        $("input[name=foodImg]").val(res.data);
                    }
                }
                ,error: function(){
                    //请求异常回调
                }
            });


            //自定义验证规则
            form.verify({
                price:function(value){
                    if(value <= 0){
                        return '价格必须为正数';
                    }
                }
            });

            //监听提交
            form.on('submit(edit)', function(data){
                //发异步，把数据提交给java
                $.ajax({
                    type:"post",
                    url:"${pageContext.request.contextPath}/food?m=edit",
                    data: data.field,
                    dataType:"json",
                    success:function (data) {
                        layer.msg(data.msg, function () {
                            if (data.code === 200){
                                window.parent.location.reload();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            }
                        });

                    }
                });
            return false;
          });

            // 获取分类数据
            $.ajax({
                type:"post",
                url:"${pageContext.request.contextPath}/food?m=getAllFoodType",
                dataType:"json",
                success:function (data) {
                    if (data.code === 200){
                        $("select[name=foodTypeCode]").empty();
                        $(data.data).each(function () {
                            if ($("#foodTypeCodeInp").val() === this.foodTypeCode1){
                                $("select[name=foodTypeCode]").append("<option value='"+this.foodTypeCode1+"' selected>"+this.foodTypeName+"</option>")
                            }else {
                                $("select[name=foodTypeCode]").append("<option value='"+this.foodTypeCode1+"'>"+this.foodTypeName+"</option>")
                            }
                        });
                        // 重新渲染
                        form.render('select');
                    }
                }
            });
        });


    </script>

  </body>

</html>